package com.landtool.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.landtool.Netty.GlobalUtil;
import com.landtool.config.SysTemPropertyConfig;
import com.landtool.database.entity.BdDataFusion;
import com.landtool.database.service.BdDataFusionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * @Description:
 */
@Controller
public class IndexController {

    private static Logger log = LoggerFactory.getLogger(IndexController.class);

    public static Integer count = 0;

    @Autowired
    private SysTemPropertyConfig map;

    @Resource
    private BdDataFusionService bdDataFusionService;


    @Autowired
    private IndexerService indexerService;

    @Autowired
    private Shipping shipping;

    @RequestMapping("/index1")
    public String index1() {
        return "index1";
    }

    @RequestMapping("/index2")
    public String index2() {
        return "index2";
    }

    @ResponseBody
    @RequestMapping("/num")
    public String Num() {
        Integer num = GlobalUtil.channels.size();
//        Integer num=WebsocketServer.onlineCount();
        return "在线人数为：" + num;
    }


//    @Scheduled(fixedDelay = 10 * 1000)
//    public void TuiSong() {
////        Shipping.GetRESTInfo(map,count);
//        Shipping.GetJson(map,count);
//        count++;
//        if(count>map.getPushNum()){
//            if(map.getElasticsearch().getOpen().equals("true")){
//                indexerService.bulkIndex(Shipping.strCurMB);
//            }
//            count=0;
//        }
//    }

    @Scheduled(fixedDelay = 10 * 1000)
    public void TuiSong() {
        shipping.GetRESTInfo(map,count);
        indexerService.TagetChangesSave(Shipping.strCurMB);
        //推送前端和后端
//        Shipping.GetJson(map,count);
        count++;
        if(count>map.getPushNum()){
            if(map.getElasticsearch().getOpen().equals("true")){
                //es批量推送
                indexerService.bulkIndex(Shipping.strCurMB);
            }
            count=0;
        }
    }

    @Scheduled(fixedDelay = 120 * 1000)
    public void GetRecentList() {
        List <BdDataFusion> list=bdDataFusionService.GetRecentList();
        HashMap <String, BdDataFusion> hmap = new HashMap <String, BdDataFusion>();
        for (int i = 0; i < list.size(); i++) {
            hmap.put(String.valueOf(list.get(i).getTargetId()), list.get(i));
        }
        Shipping.RecentList=hmap;
    }

    @RequestMapping("/getTargetById")
    @ResponseBody
    public String getTargetById(int id) {
        String A="";
        JSONArray jsonArray = Shipping.QianTaiarrMBList;
        try {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObjectA = (JSONObject) jsonArray.get(i);
                if(jsonObjectA.getInt("radar_id")==id){
                    A="{\"code\":0,\"msg\":\"操作成功\",\"data\":"+jsonObjectA.toString()+"}";
                }
            }
        }catch (NullPointerException e){
            log.error("程序没有获取到数据");
        }
        return A;
    }
}

